/*
 * @Description: 生成Mapper
 * @Author: DHL
 * @Date: 2022-01-30 14:20:12
 * @LastEditors: DHL
 * @LastEditTime: 2022-11-11 15:34:25
 */

import { firstToUpperCase, firstToLowerCase, getTsType } from '../../../utils/codeGenUtils'

export function createModel(codeGenConfig: any) {
  const classNameFirstToUpperCase = firstToUpperCase(codeGenConfig.className)
  const classNameFirstToLowerCase = firstToLowerCase(codeGenConfig.className)

  return `
/*
 * @Description: ${codeGenConfig.genName} - 模型
 * @Author: ${codeGenConfig.genAuthor}
 * @Date: ${codeGenConfig.createData}
 * @LastEditors: ${codeGenConfig.genAuthor}
 * @LastEditTime: ${codeGenConfig.createData}
 */

import { BaseModel, PageParams, Params, Result, ResultPage } from '/@/model/baseModel'

/**
 * ${codeGenConfig.genName}对象
 */
export interface ${classNameFirstToUpperCase}Model extends BaseModel {
  ${getField(codeGenConfig.optionsJson.columns)}
}

//////////////////////////////////////////////////
// 参数
//////////////////////////////////////////////////

/**
 * 查询参数
 */
export interface ${classNameFirstToUpperCase}Params extends Params {
  /**
   * 关键字模糊查询
   */
  queryLikeStr?: string
}

/**
 * 分页查询参数
 */
export interface ${classNameFirstToUpperCase}PageParams extends ${classNameFirstToUpperCase}Params, PageParams {}

//////////////////////////////////////////////////
// 返回结果
//////////////////////////////////////////////////

/**
 * 分页查询返回结果
 */
export type ${classNameFirstToUpperCase}PageResultModel = ResultPage<${classNameFirstToUpperCase}Model>

/**
 * 不分页查询返回结果
 */
export type ${classNameFirstToUpperCase}ListResultModel = Result<Array<${classNameFirstToUpperCase}Model>>


/**
 * 详情查询返回结果
 */
export type ${classNameFirstToUpperCase}ResultModel = Result<${classNameFirstToUpperCase}Model>
`
}

/**
 * 获取主键字段
 * @param columns
 */
function getField(columns: any) {
  let field: Array<string> = []

  columns.forEach((item: any) => {
    if (item.javaBean && 'version' !== item.javaField) {
      if (item.columnKey === '1') {
        field.push(
          `
  /** ${item.javaComment} */
  ${item.javaField}?: ${getTsType(item.javaType)}
  `
        )
      } else {
        field.push(
          `
  /** ${item.javaComment} */
  ${item.javaField}?: ${getTsType(item.javaType)}
  `
        )
      }
    }
  })
  return field.join('')
}
